-
Definitions:
-
"Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code."
-
"Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or in added complexity to the design due to inflexibility in the creation procedures. Creational design patterns solve this problem by somehow controlling this object creation."
-
Dependency Injection
-
A design pattern where a class receives its dependencies from the outside, instead of creating them internally.
-
Reduces coupling between classes, making the code more flexible and testable.
-
Makes it easy to swap implementations without modifying the main class.
-
Without Subtype Polymorphism :
-
.
-
-
With Subtype Polymorphism :
-
.
-
Singleton Pattern
-
"The pattern ensures that the class has only one instance and provides a global access point to it".
-
-
The presenter warns against using this method, because it is problematic, etc.
-
Factory Method Pattern
-
The pattern is based on creating a method that governs how the instantiation of other objects will be done.
-
Different 'factories' can have different logic for instantiating objects.
-
-
He gave the example of "wanting to instantiate animals randomly in a forest".
-
-
Review :
-
This pattern only makes sense in situations where you want to create an object but you don't exactly know which parameters govern the creation of that object, leaving that responsibility to the chosen 'factory'.
-
The pattern sounds useful but extremely specific to random generation, which makes it easy to forget.
-
Abstract Factory Pattern
-
It is very similar to the Factory Method, but it is based on creating multiple objects instead of single objects.
-
Review :
-
Same review as the Factory Method Pattern.
-